package com.example.studentportrait.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.studentportrait.constants.SystemConstants;
import com.example.studentportrait.domain.ResponseResult;
import com.example.studentportrait.domain.dto.AnnouncementListDto;
import com.example.studentportrait.domain.entity.Announcement;
import com.example.studentportrait.domain.entity.User;
import com.example.studentportrait.domain.vo.PageVo;
import com.example.studentportrait.enums.AppHttpCodeEnum;
import com.example.studentportrait.exception.SystemException;
import com.example.studentportrait.mapper.AnnouncementMapper;
import com.example.studentportrait.service.AnnouncementService;
import com.example.studentportrait.utils.RedisCache;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service("announcementService")
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements AnnouncementService {
    @Resource
    private RedisCache redisCache;


    //分页查询公告
    @Override
    public ResponseResult announcementList(Integer pageNum, Integer pageSize, AnnouncementListDto announcementListDto) {
        // 查询基础用户信息
        LambdaQueryWrapper<Announcement> lqw = new LambdaQueryWrapper<>();
        // 查询是否删除状态
        lqw.eq(Announcement::getIsDelete, SystemConstants.NOT_DELETE);

        // 根据账号，公告内容，账号状态查询
        lqw.eq(announcementListDto.getAccount() != null && !announcementListDto.getAccount().isEmpty(), Announcement::getAccount, announcementListDto.getAccount());
        lqw.eq(announcementListDto.getStatus() != null, Announcement::getStatus, announcementListDto.getStatus());
        lqw.like(StringUtils.hasText(announcementListDto.getContent()), Announcement::getContent, announcementListDto.getContent());
        //        分页查询
        Page page=new Page(pageNum,pageSize);
        page(page,lqw);

        // 获取查询结果
        List<User> announcementList = page.getRecords();

        // 返回分页结果
        PageVo pageVo = new PageVo(announcementList, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    //新增公告
    @Override
    public ResponseResult addAnnouncement(Announcement announcement) {
        // 内容不合理则抛异常
        if (!StringUtils.hasText(announcement.getContent()) || announcement.getStatus() <= 0) {
            throw new SystemException(AppHttpCodeEnum.ANNOUNCEMENT_NOT_NULL);
        }
        String account=redisCache.getCacheObject("account");
        System.out.println(account);
        announcement.setAccount(account);
//        新增tag
        save(announcement);
        return ResponseResult.okResult();
    }

    //删除公告
    @Override
    public ResponseResult deleteAnnouncement(Long announcementId) {
        //        id合理性判断
        if(Objects.nonNull(announcementId)&&announcementId>0){
            removeById(announcementId);
        }else{
            throw new SystemException((AppHttpCodeEnum.ID_NOT_NULL));
        }
        return ResponseResult.okResult();
    }

    //更新公告
    @Override
    public ResponseResult<Announcement> updateAnnouncementById(Announcement announcement) {
        UpdateWrapper<Announcement> updateWrapper=new UpdateWrapper<>();

//        仅设置想要更新的字段
        updateWrapper.eq("id",announcement.getId())
                .set(announcement.getContent()!=null,"content",announcement.getContent())
                .set(announcement.getStatus()>=0,"status",announcement.getStatus());
//        传入更新条件进行更新
        update(updateWrapper);
        return ResponseResult.okResult();
    }

    //批量删除公告
    @Override
    public ResponseResult deleteAnnouncementIds(List<Long> ids) {
        // 使用 MyBatis-Plus 的 removeByIds 方法来批量删除
        boolean result = removeByIds(ids);

        if (result) {
            return ResponseResult.okResult("公告删除成功");
        } else {
            throw new SystemException(AppHttpCodeEnum.ANNOUNCEMENT_DELETE_FAIL);
        }
    }
}
